from zhipuai import ZhipuAI
import re

client = ZhipuAI(api_key="e896525e15c4856390a089cc7e199c46.NU3hO2WLngbwkXmV")



def title(dict_str):
    response = client.chat.completions.create(
        model="glm-4",  # 请填写您要调用的模型名称
        messages=[
            {"role": "user","content": f"你是一名资深的亚马逊女士服装类目文案专家，f{dict_str}这个是你的产品资料"},
            {"role": "user","content": f"要求：1、吸引人的注意力，整个标题语言生动有感染力能激发消费者的购买意欲。2、包含40至55个字符"},
            {"role": "user", "content": "根据资料和要求写一个产品标题，并翻译成英文保存到变量zyi"},

        ],
    )
    response_str = response.choices[0].message.content
    print("原参数为：",response.choices[0].message)
    print("返回结果为：",response_str)
    regex =  r'zyi = "(.*?)"'
    # 使用re.search查找匹配的字符串
    match = re.search(regex,response_str, re.DOTALL)

    #如果找到匹配项，则提取数组内容
    if match:
        zyi_value = match.group(1)
        print("产品标题为：",zyi_value)
        return zyi_value
    else:
        print("没有找到匹配的产品标题为")
        return None

if __name__ == '__main__':
    dict_str = {'产品名': 'White Plaid Bubble Sleeve Flowy Shirt Dress', '价格': '17.25', '重量': '0.45 kg', '材质': '50%Cotton+45%Polyester+5%Viscose', '细节': 'This plaid dress makes you stand out in style.\nTrendy bubble sleeves add a playful and chic touch to the dress.\nHigh waist detail for a flattering and comfortable fit.\nButtoned front and shirt collar for a polished and versatile look.', '属性值': [{'尺寸': '(US 4-6)S'}, {'尺寸': '(US 8-10)M'}, {'尺寸': '(US 12-14)L'}, {'尺寸': '(US 16-18)XL'}], '规格': [['Item No', 'LC6121673-P122'], ['Product Weight', '0.45Kg'], ['Category', 'Dresses > Mini Dresses'], ['Creation Time', '2024-08-01'], ['Material', '50%Cotton+45%Polyester+5%Viscose'], ['Sleeve Length', 'Long Sleeve'], ['Waist Line', 'High Waist'], ['Style', 'casual'], ['Silhouette', 'Shift'], ['Occasion', 'Daily'], ['Dresses Length', 'Mini'], ['Color', 'white'], ['Pattern', 'plaid'], ['Neckline', 'turn-down collar']]}

    title(dict_str)